from __future__ import unicode_literals
import os
import sys
import json
import hashlib
from typing import Match
import numpy as np
import pandas as pd

with open("./configs.json","r",encoding="utf-8") as f:
    cfg = json.load(f)

if  os.path.exists (cfg["md5File"]):
    with open(cfg["md5File"],"r",encoding="utf-8") as f:
        md5 = json.load(f)
else:
    md5 = {}

#xlsx 文件名列表
def xlsxList(dir):

    fs = os.listdir(dir)
    ls = []

    for name in fs:
        if ".xlsx" in name :
            if not "~$" in name:
                ls.append(name)
            else:
                print("read file",name)

    return ls

#xlsx 生成MD5码
def xlsxMd5(dir,name):
    path = os.path.join(dir,name)
    with open(path,'rb') as fp:
        data = fp.read()

    return hashlib.md5(data).hexdigest()


def xlshSheet(pt,sname,name):
    df = pd.read_excel(pt, sheet_name = sname)
    ats = []
    max = 0
    for column in df :
        max = max
        arr = np.array(df.get(column)).tolist()
        arr.insert(0,column)
        ats.append(arr)

    bts = []
    bnt = 0
    for cms in ats :
        cnt = 0
        for row in cms:
            cnt=cnt+1
            if bnt < cnt:
                bnt = bnt + 1
                bts.append([])

            if not pd.isna(row):
                bts[cnt-1].append(row)
            else:
                bts[cnt-1].append("NaN")


    print(name+"->"+sname)
    js = json.dumps(bts, ensure_ascii=False,indent=4)
    file = open(cfg["jsonDir"] + sname + ".json","w",encoding="utf-8")
    file.write(js)
    file.close()

#xlsx 遍历解析文件
def xlsxPairs(dir):
    ls = xlsxList(dir)

    new = {}
    for name in ls:
        code = xlsxMd5(dir,name)
        if not (code in md5.keys()) or md5[code]["name"] != name:
            sheets = []
            pt = os.path.join(dir,name)
            df = pd.read_excel(pt,sheet_name=None)
            for sname in df.keys():
                sheets.append(sname)
                md5[code] = {"name":name,"sheets":sheets}
                ft = cfg["filters"]
                if (ft in "") or (ft in sname):
                    xlshSheet(pt,sname,name)
                    new[sname] = True
                
            

    js = json.dumps(md5, ensure_ascii=False,indent=4)
    file = open(cfg["md5File"],"w",encoding="utf-8")
    file.write(js)
    file.close()
    js = json.dumps(new, ensure_ascii=False,indent=4)
    file = open(cfg["newFile"],"w",encoding="utf-8")
    file.write(js)
    file.close()

xlsxPairs(cfg["xlsxDir"])

os.system("lua ./api/excel.lua")